using System;
using System.IO;
using System.Text;

namespace Caramel.IO
{
	public static class FileExtensions
	{
		public static Stream Open(this IFile file, string path, FileMode mode)
		{
			return
				file.
					Open(
						path,
						mode,
						(mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite,
						FileShare.None
					);
		}

		public static Stream Open(this IFile file, string path, FileMode mode, FileAccess access)
		{
			return
				file.
					Open(
						path,
						mode,
						access,
						FileShare.None
					);
		}

		public static Stream OpenRead(this IFile file, string path)
		{
			return
				file.
					Open(
						path,
						FileMode.Open,
						FileAccess.Read,
						FileShare.Read
					);
		}

		public static StreamReader OpenText(this IFile file, string path)
		{
			return
				new StreamReader(
					file.
						Open(
							path,
							FileMode.OpenOrCreate,
							FileAccess.Write,
							FileShare.None
						)
				);
		}

		public static Stream OpenWrite(this IFile file, string path)
		{
			return 
				file.
					Open(
						path, 
						FileMode.OpenOrCreate, 
						FileAccess.Write, 
						FileShare.None
					);
		}

		public static string[] ReadAllLines(this IFile file, string path)
		{
			return file.ReadAllLines(path, Encoding.UTF8);
		}

		public static string ReadAllText(this IFile file, string path)
		{
			return file.ReadAllText(path, Encoding.UTF8);
		}

		public static void Replace(this IFile file, string sourceFileName, string destinationFileName, string destinationBackupFileName)
		{
			file.
				Replace(
					sourceFileName, 
					destinationBackupFileName,
					destinationBackupFileName, 
					false
				);
		}

		public static void WriteAllLines(this IFile file, string path, string[] contents)
		{
			file.WriteAllLines(path, contents, Encoding.UTF8);
		}

		public static void WriteAllText(this IFile file, string path, string contents)
		{
			file.WriteAllText(path, contents, Encoding.UTF8);
		}
	}
}
